package handler;

import java.util.Date;

import bean.Client;
import bean.RequestData;
import bean.ResponseData;

import com.gmail.dengtao.joe.transport.handler.Handler;
import com.gmail.dengtao.joe.transport.session.IdleStatus;
import com.gmail.dengtao.joe.transport.session.Session;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import common.Common;
import frame.ClientFrame;


public class ClientHandler implements Handler {
	private Client client=new Client();
	private ClientFrame clientFrame;
	private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
	
	@Override
	public void sessionCreated(Session session) throws Exception {
		System.out.println("sessionCreated:"+session);
		
	}

	@Override
	public void sessionOpened(Session session) throws Exception {
		client.setSession(session);
		if(clientFrame!=null){			
			clientFrame.showMessage(Common.SYSTEM_NAME,"连接服务器成功", new Date());
		}
		System.out.println("open");
	}

	@Override
	public void sessionClosed(Session session) throws Exception {
		client.setSession(null);
		
	}

	@Override
	public void sessionIdle(Session session, IdleStatus status)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void exceptionCaught(Session session, Throwable cause) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dataReceived(Session session, Object data) throws Exception {
		String json=new String((byte[]) data);
		ResponseData responseData=gson.fromJson(json,ResponseData.class);
		if(responseData.getCommand()==ResponseData.INTI){
			client.setIdentify(responseData.getIdentify());
			client.setStatus(Client.IDLE);
			clientFrame.setClient(client);
			clientFrame.showMessage(Common.SYSTEM_NAME,responseData.getContent(), responseData.getTime());
		}
		if(responseData.getCommand()==ResponseData.CHAT){
			clientFrame.showMessage("玩家"+responseData.getIdentify(),responseData.getContent(), responseData.getTime());
		}
		if(responseData.getCommand()==ResponseData.RESPONSE_FIRST){
			client.setStatus(Client.TURN);
			clientFrame.showChess(responseData.getChess(),0,0);
			clientFrame.showMessage(Common.SYSTEM_NAME, responseData.getContent(), responseData.getTime());
		}
		if(responseData.getCommand()==ResponseData.RESPONSE_SECEND){
			client.setStatus(Client.FIGHTING);
			clientFrame.showChess(responseData.getChess(),0,0);
			clientFrame.showMessage(Common.SYSTEM_NAME, responseData.getContent(), responseData.getTime());
		}
		if(responseData.getCommand()==ResponseData.RESPONSE_TURN){
			client.setStatus(Client.TURN);
			clientFrame.showChess(responseData.getChess(),responseData.getPosX(),responseData.getPosY());
		}
		if(responseData.getCommand()==ResponseData.RESPONSE_WAIT){
			client.setStatus(Client.FIGHTING);
			clientFrame.showMessage(Common.SYSTEM_NAME, responseData.getContent(), responseData.getTime());
		}
		if(responseData.getCommand()==ResponseData.RESPONSE_FAILED){
			client.setStatus(Client.FAILED);
			clientFrame.showChess(responseData.getChess(),responseData.getPosX(),responseData.getPosY());
			clientFrame.showMessage(Common.SYSTEM_NAME, responseData.getContent(), responseData.getTime());
		}
		if(responseData.getCommand()==ResponseData.RESPONSE_WIN){
			client.setStatus(Client.WIN);
			clientFrame.showMessage(Common.SYSTEM_NAME, responseData.getContent(), responseData.getTime());
		}
		System.out.println("Received:" + new String((byte[]) data));
		
	}

	@Override
	public void dataNotSent(Session session, Object data) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dataSent(Session session, Object data) throws Exception {
		System.out.println("Received:" + new String((byte[]) data));
		
	}

	public void sendMessage(RequestData requestData){
		if(client.getSession()!=null){			
			client.getSession().send(gson.toJson(requestData).getBytes());
		}
	}
	
	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public void setClientFrame(ClientFrame clientFrame){
		this.clientFrame=clientFrame;
	}
	
}
